package model

import (
	"gin/common"
	Mysql "gin/database"
	"github.com/Gre-Z/common/jtime"
	"gorm.io/datatypes"
	"strings"
)

type SentenceWord struct {
	Id           uint64           `json:"id" gorm:"column:id;primaryKey;type:int;autoIncrement"`
	BeginOffset  uint64           `json:"begin_offset" gorm:"column:begin_offset;type:int"`
	Word         string           `json:"word" gorm:"column:word"`
	Length       uint64           `json:"length" gorm:"column:length;type:int"`
	Pos          string           `json:"pos" gorm:"column:pos"`
	SentenceId   uint64           `json:"sentence_id" gorm:"column:sentence_id;type:int"`
	Created      jtime.TstampTime `json:"created" gorm:"column:created;autoCreateTime;type:int"`
	SimilarWords datatypes.JSON   `json:"similar_words" gorm:"column:similar_words;default:\"[]\""`
	WordSpeech   WordSpeech       `json:"word_speech" gorm:"foreignKey:code;references:pos"`
}

// 设置表名
func (SentenceWord) TableName() string {
	return "sentence_word"
}

func (this *SentenceWord) Insert() (id uint64, err error) {
	result := Mysql.DB.Create(&this)
	id = this.Id
	if result.Error != nil {
		err = result.Error
		return
	}
	return
}

func (this *SentenceWord) Find(Param common.SelectQueryParam) (resData SentenceWord, err error) {

	dictOne := new(SentenceWord)
	db := Mysql.DB

	// 组装过滤条件
	if Param.Filter.Filter != nil {
		for key, val := range Param.Filter.Filter {
			vals := val.(map[string]interface{})
			if strings.Contains(key, "bw_") {
				db = db.Where(vals["condition"], vals["val1"], vals["val2"])
			} else {
				db = db.Where(vals["condition"], vals["val"])
			}
		}
	} else {
		db = db.Where("?", "1=1")
	}

	// 应用分页
	db = db.Select(Param.Fields)

	result := db.Preload("WordSpeech").First(&dictOne)
	if result.Error != nil {
		err = result.Error
	}

	return *dictOne, err
}

func (this *SentenceWord) Select(Param common.SelectQueryParam) (total int64, resData []SentenceWord, err error) {

	models := make([]SentenceWord, 0)
	db := Mysql.DB

	// 组装过滤条件
	if Param.Filter.Filter != nil {
		for key, val := range Param.Filter.Filter {
			vals := val.(map[string]interface{})
			if strings.Contains(key, "bw_") {
				db = db.Where(vals["condition"], vals["val1"], vals["val2"])
			} else {
				db = db.Where(vals["condition"], vals["val"])
			}
		}
	} else {
		db = db.Where("?", "1=1")
	}

	// 获取总条数
	db.Model(&SentenceWord{}).Count(&total)

	// 查找所有数据

	// 应用分页
	db = db.Preload("WordSpeech").Select(Param.Fields).Limit(Param.Limit).Offset(Param.Offset)

	// 应用排序参数
	if Param.Order != "" {
		db = db.Order(Param.Order)
	}

	result := db.Find(&models)
	if result.Error != nil {
		err = result.Error
		return
	}

	return total, models, err
}

func (this *SentenceWord) FindById(id uint64) (resData SentenceWord, err error) {

	dictOne := new(SentenceWord)
	db := Mysql.DB

	db = db.Preload("WordSpeech").Where("id = ?", id)

	// 应用分页
	db = db.Select("*")

	result := db.First(&dictOne)
	if result.Error != nil {
		err = result.Error
		return *dictOne, err
	}

	return *dictOne, err
}

// 更新dict
func (this *SentenceWord) Update(dict SentenceWord) (SentenceWord, error) {
	db := Mysql.DB
	result := db.Updates(&dict)
	if result.Error != nil {
		return dict, result.Error
	}
	return this.FindById(dict.Id)
}

func (this *SentenceWord) Delete(dict SentenceWord) {
	Mysql.DB.Delete(&dict)
}

// 批量删除
func (this *SentenceWord) DeleteAll(where string) {
	Mysql.DB.Where(where).Delete(&SentenceWord{})
	return
}

func (this *SentenceWord) InsertAll(sentenceWords *[]SentenceWord) {
	Mysql.DB.Create(sentenceWords)
	return
}

// 批量删除
func (this *SentenceWord) DeleteByFilter(param *common.Filter) {
	db := Mysql.DB

	if param.Filter == nil {
		return
	}
	// 组装过滤条件

	for key, val := range param.Filter {
		vals := val.(map[string]interface{})
		if strings.Contains(key, "bw_") {
			db = db.Where(vals["condition"], vals["val1"], vals["val2"])
		} else {
			db = db.Where(vals["condition"], vals["val"])
		}
	}
	db.Delete(SentenceWord{})

}
